#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time   :  2019/7/4 10:50
@Author :  geqh
@file   :  tag_EN.py
"""
import nltk
from utils.log import *
from utils.confPaser import *
import os

nltk.download("punkt")
nltk.download("averaged_perceptron_tagger")


def load_dict():
    dic_path = os.path.join(project_path, "conf/dic_en.txt")
    map = {}
    with open(dic_path, "r", encoding="utf8") as f:
        lines = f.readlines()
    for line in lines:
        one = line.split("-")
        items = one[1].split(";")
        for item in items:
            map[item.strip("\n")] = one[0]
    logger.info("load_dict:"+dic_path)
    return map


def tagEN(text, dict_map):
    english_punctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']',
                            '&', '!', '*', '@', '#', '$', '%', '<', '>', '\'',
                            '\"', '\\', '/']
    words = nltk.word_tokenize(text)
    pos_tags = nltk.pos_tag(words)
    tags = []
    for pos_tag in pos_tags:
        tag = str(pos_tag[1]).upper().strip()
        word = pos_tag[0].strip()
        if dict_map.get(tag, 0):
            tags.append({"word": word, "tag": tag, "pos": dict_map[tag]})
        elif word in english_punctuations:
            tags.append({"word": word, "tag": tag, "pos": "标点"})
        else:
            tags.append({"word": word, "tag": tag, "pos": "X"})

    logger.info(tags)
    return tags
